import Vue from "vue"
import Vuex from "vuex"
import persistedState from 'vuex-persistedstate'
// 可以使用 Router.push({}) message({})
import { Message } from "element-ui"
import { message1 } from '@/api'
import { message } from '@/api'
import router from "@/router"

Vue.use(Vuex);


// state存储状态的  数据  
const state = {   //获取n  this.$store.state.n
  token: '',
  id: '',
  userImg: '',
  userphone: '',
  money:'',
}

const mutations = {   // this.$store.commit
  // token
  SET_TOKEN(state, token) {
    state.token = token
  },
  // id
  SET_ID(state, id) {
    state.id = id
  },
  // userImg
  SET_IMG(state, userImg) {
    state.userImg = userImg
  },
  // userphone
  SET_USERPHONE(state, userphone) {
    state.userphone = userphone
  },
  SET_MONEY(state, money) {
    state.money = money
  },
  CHANGE_MONEY(state,money) {
    state.money = money
  }
}

const actions = {   //执行上下文 this.$store.dispatch('login_action')
  login(context, user) {
    let { userphone, password } = user;
    message1({ userphone, password }).then(res => {
      // console.log(res, 'res');
      if (res.data.ststus === 1) {
        context.commit("SET_TOKEN", res.data.msg)
        context.commit("SET_ID", res.data.data.uid)
        context.commit("SET_IMG", res.data.data.userImg)
        context.commit("SET_USERPHONE", res.data.data.userphone)
        context.commit("SET_MONEY", res.data.data.userMoney)
        Message({
          message: '登录成功,2秒后跳转到首页',
          type: 'success',
          duration: 1500
        })
        setTimeout(() => {
          router.push('/home')
        }, 2000)
      } else if (res.data.msg === "账号或秘密输入有误") {
        Message({
          message: '用户密码错误',
          type: 'error',
          duration: 1500
        })
      } else {
        Message({
          message: '用户未注册',
          type: 'error',
          duration: 1500
        })
      }
    })
  },
  login2(context, user) {
    let { userphone, code } = user;
    message({ userphone, code }).then(res => {
      // console.log(res.data, 'res')
      if (res.data.ststus === 1) {
        context.commit("SET_TOKEN", res.data.msg)
        context.commit("SET_ID", res.data.data.uid)
        context.commit("SET_IMG", res.data.data.userImg)
        context.commit("SET_USERPHONE", res.data.data.userphone)
        context.commit("SET_MONEY", res.data.data.userMoney)
        Message({
          message: '登录成功,2秒后跳转到首页',
          type: 'success',
          duration: 1500
        })
        setTimeout(() => {
          router.push('/home')
        }, 2000)
      } else if (res.data.msg == '用户名不存在，登陆失败') {
        Message({
          message: '用户名不存在，登陆失败',
          type: 'error',
          duration: 1500
        })
      } else if (res.data.msg == '验证码输入有误') {
        Message({
          message: '验证码输入有误',
          type: 'error',
          duration: 1500
        })
      }
    })
  },
  logOut(context) {
    context.commit("SET_TOKEN", '')
    context.commit("SET_ID", '')
    context.commit("SET_IMG", '')
    context.commit("SET_USERPHONE", '')
    Message({
      message: '退出成功',
      type: 'success'
    })
    router.push('/')
  },
  changeMoney(context,money) {
    context.commit("CHANGE_MONEY", money)
  }
}

// vuex的计算属性
const getters = {   // this.$store.getters.   mapGetters
}
//仓促
const store = new Vuex.Store({
  state,
  mutations,
  actions,
  getters,
  plugins: [
    persistedState({ storage: window.sessionStorage })
  ] //单独插件  封装本地存储
})

export default store;